using Microsoft.AspNetCore.Mvc;
using RAG.Application.Services.Interfaces;
using RAG.Application.Dtos.Permission;

namespace RAG.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class PermissionController : ControllerBase
{
    private readonly IPermissionService _permissionService;

    public PermissionController(IPermissionService permissionService)
    {
        _permissionService = permissionService;
    }

    /// <summary>
    /// 获取所有权限
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<PermissionDto>>> GetAllPermissions()
    {
        try
        {
            var permissions = await _permissionService.GetAllPermissionsAsync();
            return Ok(permissions);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "获取权限列表失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 根据ID获取权限
    /// </summary>
    [HttpGet("{id}")]
    public async Task<ActionResult<PermissionDto>> GetPermissionById(Guid id)
    {
        try
        {
            var permission = await _permissionService.GetPermissionByIdAsync(id);
            if (permission == null)
            {
                return NotFound(new { message = "权限不存在" });
            }

            return Ok(permission);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "获取权限失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 创建权限
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<PermissionDto>> CreatePermission([FromBody] PermissionDto permissionDto)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var createdPermission = await _permissionService.CreatePermissionAsync(permissionDto);
            return CreatedAtAction(nameof(GetPermissionById), new { id = createdPermission.Id }, createdPermission);
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(new { message = ex.Message });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "创建权限失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 更新权限
    /// </summary>
    [HttpPut("{id}")]
    public async Task<ActionResult<PermissionDto>> UpdatePermission(Guid id, [FromBody] PermissionDto permissionDto)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var updatedPermission = await _permissionService.UpdatePermissionAsync(id, permissionDto);
            return Ok(updatedPermission);
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(new { message = ex.Message });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "更新权限失败", error = ex.Message });
        }
    }

    /// <summary>
    /// 删除权限
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<ActionResult> DeletePermission(Guid id)
    {
        try
        {
            var result = await _permissionService.DeletePermissionAsync(id);
            if (!result)
            {
                return NotFound(new { message = "权限不存在" });
            }

            return NoContent();
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(new { message = ex.Message });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { message = "删除权限失败", error = ex.Message });
        }
    }
} 